package com.winderinfo.yikaotianxia.ui.itembank;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.winderinfo.yikaotianxia.R;

import java.util.List;

public class FreeListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<FreeBean.RowsBean> list;

    public FreeListAdapter(Context context, List<FreeBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_itembank, viewGroup, false);
        SelectedViewHolder selectedViewHolder = new SelectedViewHolder(view);
        return selectedViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        SelectedViewHolder selectedViewHolder = (SelectedViewHolder) viewHolder;
        Glide.with(context)
                .load(list.get(i).getYkProfessional().getPhoto())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))//圆角半径
                .into(selectedViewHolder.ite_image);
        selectedViewHolder.ite_title.setText(list.get(i).getTitleTitl());
        selectedViewHolder.ite_time.setText(list.get(i).getCreatetime());
        //接口回调
        selectedViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onTiId!=null){
                    int proId = list.get(i).getId();
                    onTiId.getId(proId);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class SelectedViewHolder extends RecyclerView.ViewHolder {

        private final TextView ite_time, ite_title;
        private final ImageView ite_image;

        public SelectedViewHolder(@NonNull View itemView) {
            super(itemView);
            ite_time = itemView.findViewById(R.id.ite_time);
            ite_title = itemView.findViewById(R.id.ite_title);
            ite_image = itemView.findViewById(R.id.ite_image);
        }
    }

    //接口回调
    public interface OnTiId {
        void getId(int id);
    }

    public OnTiId onTiId;

    public void setOnTiId(OnTiId onTiId) {
        this.onTiId = onTiId;
    }

}
